﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

namespace Code.Entity.Migrations
{
    public partial class _10181 : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropPrimaryKey(
                name: "PK_Admins",
                table: "Admins");

            migrationBuilder.RenameTable(
                name: "Admins",
                newName: "ce_Admin");

            migrationBuilder.AlterColumn<string>(
                name: "UserName",
                table: "ce_Admin",
                type: "nvarchar(50)",
                maxLength: 50,
                nullable: false,
                defaultValue: "",
                oldClrType: typeof(string),
                oldType: "nvarchar(max)",
                oldNullable: true);

            migrationBuilder.AlterColumn<string>(
                name: "Password",
                table: "ce_Admin",
                type: "nvarchar(50)",
                maxLength: 50,
                nullable: false,
                defaultValue: "",
                oldClrType: typeof(string),
                oldType: "nvarchar(max)",
                oldNullable: true);

            migrationBuilder.AddColumn<string>(
                name: "Avatar",
                table: "ce_Admin",
                type: "nvarchar(100)",
                maxLength: 100,
                nullable: true);

            migrationBuilder.AddColumn<DateTime>(
                name: "CreateTime",
                table: "ce_Admin",
                type: "datetime2",
                nullable: false,
                defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

            migrationBuilder.AddColumn<string>(
                name: "LastLoginIP",
                table: "ce_Admin",
                type: "nvarchar(10)",
                maxLength: 10,
                nullable: true);

            migrationBuilder.AddColumn<DateTime>(
                name: "LastLoginTime",
                table: "ce_Admin",
                type: "datetime2",
                nullable: true);

            migrationBuilder.AddPrimaryKey(
                name: "PK_ce_Admin",
                table: "ce_Admin",
                column: "AdminId");

            migrationBuilder.CreateTable(
                name: "ce_Role",
                columns: table => new
                {
                    RoleId = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    RoleName = table.Column<string>(type: "nvarchar(50)", maxLength: 50, nullable: false),
                    CreateTime = table.Column<DateTime>(type: "datetime2", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ce_Role", x => x.RoleId);
                });

            migrationBuilder.CreateTable(
                name: "ce_AdminRole",
                columns: table => new
                {
                    Id = table.Column<int>(type: "int", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    AdminId = table.Column<int>(type: "int", nullable: false),
                    RoleId = table.Column<int>(type: "int", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_ce_AdminRole", x => x.Id);
                    table.ForeignKey(
                        name: "FK_ce_AdminRole_ce_Admin_AdminId",
                        column: x => x.AdminId,
                        principalTable: "ce_Admin",
                        principalColumn: "AdminId",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_ce_AdminRole_ce_Role_RoleId",
                        column: x => x.RoleId,
                        principalTable: "ce_Role",
                        principalColumn: "RoleId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_ce_AdminRole_AdminId",
                table: "ce_AdminRole",
                column: "AdminId");

            migrationBuilder.CreateIndex(
                name: "IX_ce_AdminRole_RoleId",
                table: "ce_AdminRole",
                column: "RoleId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "ce_AdminRole");

            migrationBuilder.DropTable(
                name: "ce_Role");

            migrationBuilder.DropPrimaryKey(
                name: "PK_ce_Admin",
                table: "ce_Admin");

            migrationBuilder.DropColumn(
                name: "Avatar",
                table: "ce_Admin");

            migrationBuilder.DropColumn(
                name: "CreateTime",
                table: "ce_Admin");

            migrationBuilder.DropColumn(
                name: "LastLoginIP",
                table: "ce_Admin");

            migrationBuilder.DropColumn(
                name: "LastLoginTime",
                table: "ce_Admin");

            migrationBuilder.RenameTable(
                name: "ce_Admin",
                newName: "Admins");

            migrationBuilder.AlterColumn<string>(
                name: "UserName",
                table: "Admins",
                type: "nvarchar(max)",
                nullable: true,
                oldClrType: typeof(string),
                oldType: "nvarchar(50)",
                oldMaxLength: 50);

            migrationBuilder.AlterColumn<string>(
                name: "Password",
                table: "Admins",
                type: "nvarchar(max)",
                nullable: true,
                oldClrType: typeof(string),
                oldType: "nvarchar(50)",
                oldMaxLength: 50);

            migrationBuilder.AddPrimaryKey(
                name: "PK_Admins",
                table: "Admins",
                column: "AdminId");
        }
    }
}
